package com.apps.gomdiem.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.apps.gomdiem.R;
import com.apps.gomdiem.fragment.MenuTabFragment.OnCheckBoxClickedListener;
import com.apps.gomdiem.model.MainModel;
import com.apps.gomdiem.model.MenuList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

public class ListViewAdapter extends BaseAdapter {
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	ArrayList<MenuList> arrayMenu;
	Context context;
	boolean[] checkBoxState;
	private OnCheckBoxClickedListener onCallBack;

	public ListViewAdapter(ArrayList<MenuList> textArr, Context contxt, OnCheckBoxClickedListener onCallBack) {
		arrayMenu = textArr;
		context = contxt;
		options = new DisplayImageOptions.Builder().cacheInMemory(false)
				.cacheOnDisc(false).displayer(new SimpleBitmapDisplayer())
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.showStubImage(R.drawable.ic_launcher)
				.showImageOnFail(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		checkBoxState = new boolean[arrayMenu.size()];
		this.onCallBack = onCallBack;

	}

	public void update(ArrayList<MenuList> listMenu) {
		this.arrayMenu = listMenu;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arrayMenu.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return arrayMenu.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;
		if (convertView == null) {
			LayoutInflater ltInflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			viewHolder = new ViewHolder();
			convertView = ltInflate.inflate(R.layout.custom_listview_item, null);
			viewHolder.check = (CheckBox) convertView.findViewById(R.id.check);
			viewHolder.check.setFocusable(false);
			viewHolder.check.setFocusableInTouchMode(false);
			viewHolder.img = (ImageView) convertView.findViewById(R.id.imageViewMenu);
			viewHolder.txtNameOfMenu = (TextView) convertView.findViewById(R.id.txtNameOfMenu);
			viewHolder.txtPoint = (TextView) convertView.findViewById(R.id.txtPoint);
			viewHolder.txtPrice = (TextView) convertView.findViewById(R.id.txtPrice);
			
			convertView.setTag(viewHolder);
			
			viewHolder.check.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					if (((CheckBox) v).isChecked()){
						checkBoxState[position] = true;
						//Khoahm: callback dÃ¹ng Ä‘á»ƒ xá»­ lÃ½ checked
						onCallBack.onChecked(arrayMenu.get(position));
					}
					else{
						checkBoxState[position] = false;
						//Khoahm: callback dÃ¹ng Ä‘á»ƒ xá»­ lÃ½ unchecked
						onCallBack.onUnchecked(arrayMenu.get(position));
						}
				}
			});

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.txtNameOfMenu.setText(arrayMenu.get(position).nameMenu);
		viewHolder.txtPoint.setText(Integer.toString(arrayMenu.get(position).point));
		viewHolder.txtPrice.setText(Integer.toString(arrayMenu.get(position).price));
		viewHolder.check.setChecked(arrayMenu.get(position).isSelected());
		// for managing the state of the boolean
		// array according to the state of the
		// CheckBox
		
		

		imageLoader.displayImage(arrayMenu.get(position).linkImg,
				viewHolder.img, options);
		return convertView;
	}

	private class ViewHolder {
		CheckBox check;
		ImageView img;
		TextView txtNameOfMenu;
		TextView txtPoint;
		TextView txtPrice;
	}
	
}
